#!/usr/bin/env python
#-*- coding: utf_8 -*-
#
# Copyright 2010 Stanley Cai
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__author__= "Stanley Cai"
__email__ = "stanley.w.cai@gmail.com"

import urllib2
import urllib
import sys
import os
import re

def get_url_content(url):
  count = 3
  while 1:
    try:
      ifile = urllib2.urlopen(url)
      data = ifile.read()
      # stupid tianya uses a mixed charset string
      data = data.decode("gbk", "ignore").encode("utf_8")
      ifile.close()
      break
    except:
      count -= 1
      if count == 0:
        return None
  return data
  
def get_pages(cont, url=None):
  yield cont
  while 1:
    p0 = 0
    p1 = cont.find("<table >",p0)
    if p1 < 0:
      break
    p2 = cont.find('<font color=blue>',p1)
    if p2 < 0:
      break
    p3 = cont.find('下一页',p2)
    if p3 < 0:
      break
    tmpl='href='
    p4 = cont.rfind(tmpl, p2, p3)
    if p4 < 0:
      break
    url = cont[p4+len(tmpl):p3]
    print "URL....", url
    cont = get_url_content(url)
    yield cont
  
  return

def get_author(cont):
  p1 = cont.find("<table")
  if p1 < 0:
    return None
  p1 = cont.find('>作者：<a', p1)
  if p1 < 0:
    return None
  s1 = 'target="_blank">'
  p1 = cont.find(s1, p1)
  if p1 < 0:
    return None
  print "p1", p1
  s2 = "</a>"
  p2 = cont.find(s2, p1)
  if p2 < 0:
    return None
  print "p2", p2
  return cont[p1 + len(s1) : p2]
    
def get_title(cont):
  s1 = '<title>'
  p1 = cont.find(s1)
  if p1 < 0:
    return None
  p2 = cont.find('</title>', p1)
  if p2 < 0:
    return None
  return cont[p1 + len(s1) : p2]
    
def get_by_author(cont,author):
  p0 = 0
  res = []
  while 1:
    p1 = cont.find("<table",p0)
    if p1 < 0:
      break
    p2 = cont.find(">"+author+"</a>", p1)
    if p2 < 0:
      break 
    time1 = cont.find("提交日期", p2)
    if time1 < 0:
      time1 = cont.find("回复日期", p2)
      if time1 < 0:
        break
    else:
      time2 = cont.find("回复日期", p2)
      if time2 > 0:
        if time2 < time1:
          time1 = time2
    time_end1 = cont.find("</font>", time1)
    if time_end1 < 0:
      break
    time_end2 = cont.find("<br>", time1)
    if time_end2 < 0:
      break
    if time_end2 < time_end1:
      time_end1 = time_end2
    p3 = cont.find("</table>", p2)
    if p3 < 0:
      break
    p4 = cont.find("<table", p3)
    if p4 < 0:
      p4 = cont.find("\t</div>", p3)
      assert(p4 > 0)
      res.append(cont[p3 + 8 : p4 :])
      break
    else:
      p0 = p4
      res.append("<B>" + author)
      res.append(cont[time1:time_end1])
      res.append("</B>")
      res.append(cont[p3+8:p4:])
  return res

